//
//  HOContactUsViewController.m
//  微度报修
//
//  Created by foguo-mac-1 on 7/25/16.
//  Copyright © 2016 WeiDu. All rights reserved.
//

#import "HOContactUsViewController.h"
#import "HOEditView.h"
#import "HOUtils.h"
#import "HOConstants.h"

static NSString *phoneNumber = @"0571-28207515";
@interface HOContactUsViewController ()
{
	HOEditView *feedbackEditView;
}
@end

@implementation HOContactUsViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
	[self initTableView];
	[self initSubviews];
	baseTableview.tableFooterView = [self getFooterViewWithTitle:NSLocalizedString(@"确认提交", @"确认提交")];
	baseTableview.separatorStyle = UITableViewCellSeparatorStyleNone;
	[self registerKeyboard];
	[self registerTapGesture];
}
- (void)initSubviews
{
	feedbackEditView = [[HOEditView alloc] initWithFrame:CGRectMake(0,
																	0,
																	self.view.frame.size.width,
																	134)];
	feedbackEditView.limitedCount = 120;
//	feedbackEditView.delegate = self;
	feedbackEditView.layer.cornerRadius = 0;
	feedbackEditView.layer.borderWidth = 0;
	[feedbackEditView.layer setMasksToBounds:YES];
	[feedbackEditView setPlaceHolder:NSLocalizedString(@"欢迎您提供任何意见和建议给我们", @"欢迎您提供任何意见和建议给我们")];
	[feedbackEditView getTextView].returnKeyType = UIReturnKeySend;
	
	feedbackEditView.frame = CGRectMake(HO_HORIZONAL_PADDING,
								0,
								self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
								134);
	feedbackEditView.layer.borderWidth = [HOUtils defaultOnePixelConversion];
	[feedbackEditView updateLayout];
}

- (void)viewWillAppear:(BOOL)animated
{
	self.tabBarController.tabBar.hidden = YES;
	[super viewWillAppear:animated];
	[self setNavigationItemTitle:NSLocalizedString(@"联系我们", @"联系我们")];
}

- (void)didClickFooterButton:(UIButton *)btn
{
	NSString *feedback = [self getTrimingCharaFromTextView:[feedbackEditView getTextView]];
	if(feedback.length == 0)
	{
		[self popupHintView:NSLocalizedString(@"请填写意见和建议", @"请填写意见和建议")];
		return;
	}
	
	[self sendRequestWithParams:@{kHO_FIELD_Action:kHO_ACTION_SendFeedback,
								  kHO_FIELD_Content:feedback}];
}

- (void)onKeyboardWillShow:(NSNotification *)notification
{
	[super onKeyboardWillShow:notification];
//	[baseTableview scrollToRowAtIndexPath:[NSIndexPath indexPathForRow:0 inSection:1] atScrollPosition:UITableViewScrollPositionMiddle animated:YES];
}

- (void)hideKeyboard
{
	[feedbackEditView resignFirstResponder];
	[[feedbackEditView getTextView] resignFirstResponder];
}


- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
	[self hideKeyboard];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/
#pragma mark - -- UITableViewDelegate/UITableViewDataResource implement methods
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
	if(indexPath.section == 0)
		return 134;
	else
	{
		return 44;
	}
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}


- (CGFloat) tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{
    return SECTION_PADDING / 2;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    static NSString *AMBaseTableViewCellIdentifier = @"HOTableViewCell";
    HOTableViewCell *cell = (HOTableViewCell *)[tableView dequeueReusableCellWithIdentifier:AMBaseTableViewCellIdentifier];
    if(cell == nil)
    {
        cell = [[[HOTableViewCell alloc]
                initWithStyle:UITableViewCellStyleDefault
                reuseIdentifier: AMBaseTableViewCellIdentifier] autorelease];
	}
	[cell.contentView setBackgroundColor: UIColorFromRGB(0xf9f9f9)];
	if(indexPath.section == 0)
	{
		HOEditView *view = (HOEditView *)[cell viewWithTag:112];
		if(view == nil)
		{
			feedbackEditView.tag = 112;
			[cell.contentView addSubview:feedbackEditView];
		}
	}
	else
	{
		UIView *view = (UIView *)[cell viewWithTag:113];
		if(view == nil)
		{
			view = [[[UIView alloc]
					initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
											 0,
											 self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
											 44)] autorelease];
			[view setBackgroundColor:[UIColor whiteColor]];
			view.layer.borderWidth = [HOUtils defaultOnePixelConversion];
			view.layer.borderColor = UIColorFromRGB(0xcdcdcd).CGColor;
			view.layer.cornerRadius = 2;
			[view.layer setMasksToBounds:YES];
			UIButton *btn = [[UIButton alloc]
							 initWithFrame:CGRectMake(HO_HORIZONAL_PADDING,
													  0,
													  self.view.frame.size.width - 2 * HO_HORIZONAL_PADDING,
													  44)];
			btn.titleLabel.font = [UIFont systemFontOfSize:16];
			btn.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
			[btn setTitle:[NSString stringWithFormat:@"%@%@", NSLocalizedString(@"反馈电话：", @"反馈电话："), phoneNumber] forState:UIControlStateNormal];
			[btn setTitleColor:MAIN_COLOR_NORMAL forState:UIControlStateNormal];
			[btn addTarget:self action:@selector(call:) forControlEvents:UIControlEventTouchUpInside];
			[view addSubview:btn];
			[cell.contentView addSubview:view];
		}
	}
    return cell;
}

- (void)call:(id)sender
{
	[HOUtils callPhoneNumber:phoneNumber];
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    
}
#pragma mark - -- tool

- (void)requestDidSuccessWithResponse:(HOBaseResponseEntity *)response
{
	if([response isEqualAction:kHO_ACTION_SendFeedback])
	{
		self.view.userInteractionEnabled = NO;
		[self popupHintView:NSLocalizedString(@"提交成功", @"提交成功") inTime:1.5 needCallback:YES];
	}
}

- (void)hintDidHide
{
	self.view.userInteractionEnabled = YES;
	[self goBack];
}
@end
